package com.jxsd.edu.cn.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


/**
 * 管理数据库连接的工具
 * */
public class DbTool 
{
	  private  static  String      Driver="com.mysql.jdbc.Driver";
	  private  static  String      dbUser="root";
	  private  static  String      password="sa";
	  private  static  String      dbName="jxsdmarket";
	  private  static  String      dbURL= "jdbc:mysql://localhost:3306/"+dbName+"?useUnicode=true&characterEncoding=utf8";
	  
	  
	  static
	  {
		  try {
			Class.forName(Driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }
	  //获取一个数据库连接
	  public static  Connection  getConnection() throws SQLException
	  {
		  Connection conn=DriverManager.getConnection(dbURL, dbUser, password);
		  return  conn;
	  }
	  
	  //关闭数据库连接
	  public  static void  close(Statement stmt,Connection conn,ResultSet result)
	  {
		  try {
		  if(result!=null)
			  result.close();
		  if(stmt!=null)
			  stmt.close();
		  if(conn!=null)
			 conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	   }
		  
	  }
